package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.RightsMapper;
import com.jt.pojo.Rights;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RightsServiceIml implements RightsService {
    @Autowired
    private RightsMapper rightsMapper;


    @Override
    public List<Rights> getAll() {
        return  rightsMapper.selectList(null) ;
    }

//    @Override9
//    public List<Rights> getRightsList() {
//        /*条件构造器,动态查询数据库一级菜单
//        * 想要查询二级菜单,必须先获取一级菜单
//        * */
//        QueryWrapper queryWrapper=new QueryWrapper();
//        queryWrapper.eq("preant_id", 0);
//        List <Rights>  rightsList=rightsMapper.selectList(queryWrapper);
//        for(Rights it : rightsList){ /*循环遍历得到一级菜单   it*/
//            queryWrapper.clear();/*清空查询条件*/
//            queryWrapper.eq("preant_id", 0);/*先查一级菜单*/
//            queryWrapper.eq("preant_id",it.getId()); /*查询二级菜单*/
//
//            List <Rights> twor= rightsMapper.selectList(queryWrapper);
//            it.setChildren(twor);   /*把二级菜单通过 Children属性 封装到一级菜单中*/
//        }
//        return rightsList; /*只展示一级菜单*/
//    }

    @Override
    public List<Rights> getRightsList() {
        QueryWrapper queryWrapper=new QueryWrapper();/*条件构造器*/
        queryWrapper.eq("parent_id",0 ); /*查询一级菜单*/
          List<Rights> oneList=rightsMapper.selectList(queryWrapper) ; /*获取一级菜单*/
          for(Rights oneRights:oneList){/*找二级菜单需要先拿到一级菜单再查找二级*/
              queryWrapper.clear(); /*清空条件体,只用一个条件构造器*/
              queryWrapper.eq("parent_id",oneRights.getId());/**/
              List <Rights> twoList=rightsMapper.selectList(queryWrapper); /*获取二级菜单*/
              oneRights.setChildren(twoList);
          }
          return oneList; /*返回一级菜单*/
    }
}
